package com.jdk.demo.nio.networkinterface;

import java.net.InetAddress;
import java.net.InterfaceAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.List;

public class InterfaceAddressDemo {

	// InterfaceAddress: 类中的方法可以取得网络接口对应的ip地址、子网掩码和广播地址等信息
	public static void main(String[] args) {
		try {
			Enumeration<NetworkInterface> networkInterface = NetworkInterface.getNetworkInterfaces();
			while(networkInterface.hasMoreElements()) {
				NetworkInterface eachNetworkInterface = networkInterface.nextElement();
				List<InterfaceAddress> addressList = eachNetworkInterface.getInterfaceAddresses();
				for (int i = 0; i < addressList.size(); i++) {
					InterfaceAddress eachAddress = addressList.get(i);
					InetAddress inetAddress = eachAddress.getAddress();
					InetAddress broadcastAddress = eachAddress.getBroadcast();
					eachAddress.getNetworkPrefixLength();
				}
			}
		}catch (Exception e) {
			// TODO: handle exception
		}
	}

}
